package com.qqla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qqla.context.BaseContext;
import com.qqla.dto.ChatRecordsPage;
import com.qqla.dto.InsertChatDto;
import com.qqla.entity.ChatRecords;
import com.qqla.mapper.ChatMapper;
import com.qqla.result.PageResult;
import com.qqla.service.ChatService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, ChatRecords> implements ChatService {

    /*
     * 分页获取聊天记录
     * */
    @Override
    public PageResult getChatList(ChatRecordsPage chatRecordsPage) {

        // 执行sql查询语句
        Page<ChatRecords> page = lambdaQuery()
                .eq(chatRecordsPage.getStudyId() != null, ChatRecords::getStudyId, chatRecordsPage.getStudyId())
                .orderByAsc(ChatRecords::getCreateTime)
                .page(new Page<>(chatRecordsPage.getPage(), chatRecordsPage.getPageSize()));

        PageResult result = PageResult.builder()
                .records(page.getRecords())
                .total(page.getTotal())
                .build();

        return result;
    }


    /*
     * 插入聊天数据
     * */
    @Override
    public ChatRecords insertRecords(InsertChatDto dto) {

        // 整合参数
        ChatRecords chatRecords = BeanUtil.copyProperties(dto, ChatRecords.class);
        baseMapper.insert(chatRecords);
        return chatRecords;
    }
}
